﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Gma.QrCodeNet.Encoding;
using Gma.QrCodeNet.Encoding.Windows.Render;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using NetCoreCMS.Common;
using NetCoreCMS.IServices;

namespace NetCoreCMS.Web.Controllers
{
    public class QrCodeController : Controller
    {
        public IUsersService _users { get; set; }
        public IWebHostEnvironment _env { get; set; }
        public IActionResult Index(string username,int moban=1,int level=100)
        {
            //优化加入redis 不用重复生成
            var cfg = Common.XMLConfig.Sysconfig.loadConfig(_env.ContentRootPath);
            var codeParams = CodeDescriptor.Init(ErrorCorrectionLevel.H, cfg.weburl+"/Web/Register?pid=" + username, QuietZoneModules.Two, 5);
            codeParams.TryEncode();
            // Render the QR code as an image
            using (var ms = new MemoryStream())
            {
                codeParams.Render(ms);
                Bitmap bmp = new Bitmap(ms);
                #region 加入图片合成功能
                string backimg =Path.Combine( _env.WebRootPath,@"share\img\ewm1.jpg");
                if(moban==2)
                    backimg = Path.Combine(_env.WebRootPath, @"share\img\ewm2.jpg");
                var user = _users.GetModel(username);
                if(user==null)
                {
                    user = new NetCoreCMS.Models.Users { user_name = "test", nick_name = cfg.webcompany };
                }
                //var weixin = wxbll.GetModelByUserId(user.id);
                Image wximg = null;
                //if(weixin!=null)
                //{
                //    wximg = HttpPost(weixin.headImage, string.Empty);
                //}
                bmp = (Bitmap)CombinImg.CombinImage(backimg, bmp, user.nick_name,cfg.webcompany, wximg);
                #region Compress
                ImageCodecInfo myImageCodecInfo;
                Encoder myEncoder;
                EncoderParameter myEncoderParameter;
                EncoderParameters myEncoderParameters;

                // Get an ImageCodecInfo object that represents the JPEG codec.
                myImageCodecInfo = GetEncoderInfo("image/jpeg");

                // Create an Encoder object based on the GUID

                // for the Quality parameter category.
                myEncoder = Encoder.Quality;

                // Create an EncoderParameters object.
                // An EncoderParameters object has an array of EncoderParameter
                // objects. In this case, there is only one

                // EncoderParameter object in the array.
                myEncoderParameters = new EncoderParameters(1);

                // Save the bitmap as a JPEG file with 给定的 quality level 压缩等级，0到100，0 最差质量，100 最佳
                myEncoderParameter = new EncoderParameter(myEncoder, level);
                myEncoderParameters.Param[0] = myEncoderParameter;
                #endregion
                MemoryStream ms2 = new MemoryStream();
                bmp.Save(ms2, myImageCodecInfo, myEncoderParameters);
                bmp.Dispose();
                ms2.Dispose();
                #endregion
                return new FileStreamResult(new MemoryStream(ms2.GetBuffer()), "image/jpg");
            }
        }
        private static ImageCodecInfo GetEncoderInfo(String mimeType)
        {
            int j;
            ImageCodecInfo[] encoders;
            encoders = ImageCodecInfo.GetImageEncoders();
            for (j = 0; j < encoders.Length; ++j)
            {
                if (encoders[j].MimeType == mimeType)
                    return encoders[j];
            }
            return null;
        }
    }
}